package com.duan.user.controller;

import com.duan.entity.PageResult;
import com.duan.entity.Result;
import com.duan.entity.StatusCode;
import com.duan.user.pojo.User;
import com.duan.user.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表
 */
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<User>> selectAll() {
        List<User> users = userService.selectAll();
        return Result.ok("查询成功", users);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody User user) {

        Integer insert = userService.insert(user);
        if (insert == -2) {
            return Result.fail("添加失败");
        }
        return Result.ok("新增成功");
    }

    /**
     * 修改用户表
     *
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.ok("修改成功");
    }

    /**
     * 删除用户表
     *
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids) {
        userService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 用户列表条件查询
     *
     * @param user
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<User>> select(@RequestBody User user) {
        return Result.ok("条件查询成功", userService.selectList(user));
    }


    /**
     * 用户列表分页查询
     *
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping("/pageAll")
    public Result pageAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> pageList = userService.pageAll(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }


    /**
     * 分页条件查询
     *
     * @param user
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList")
    public Result pageList(@RequestBody User user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> pageList = userService.pageList(user, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }


    /**
     * 启动、停用
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status")
    public Result updateStatus(@RequestParam Long id, @RequestParam Integer status) {
        userService.status(id, status);
        return Result.ok("修改成功");
    }

    /**
     * 重置密码
     *
     * @param ids
     * @return
     */
    @PostMapping("/reset")
    public Result reset(@RequestBody List<Long> ids) {
        userService.resetPwd(ids);
        return Result.ok("修改成功");
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String login = userService.login(user.getUsername(), user.getPassword());
        if ("-1".equals(login)) {
            return Result.fail("用户名或密码错误");
        } else if ("-2".equals(login)) {
            return Result.fail("该用户不存在");
        } else if ("-3".equals(login)) {
            return Result.fail("该用户已停用");
        }
        Map map = new HashMap();
        map.put("username", user.getUsername());
        map.put("token", login);
        return Result.ok("登录成功", map);
    }


}
